New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

indicative-parser

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

indicative-parser

Schema parser for Indicative

  • 7.1.4
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Indicative Parser

Converts indicative rules and messages schema to a tree

circleci-image npm-image license-image

Indicative parser pre-compiles the Indicative schema to a recursive tree of nodes. Each node is given one of the following types.

  • object: Node with one or more nested children.
  • array: Node with one or more index or wildcard based nested children.
  • literal: The leaf nodes.

Do note, that the literal type is not equal to literal values in Javascript. For parser, the literal nodes are nodes with no leaf.

Table of contents

Why Indicative needs a parser?

If you look at the Indicative schema, it is very concise and developer friendly. However, the same schema needs to be parsed to execute the validation rules.

{
  username: 'required',
  'account.type': 'required|in:email,social'
}

One way is to loop over the schema object keys, split them by . and then inline execute the validations for each field. This process is very straight forward, but will have performance issues.

Instead, we parse the schema into a tree. The tree is later converted to an array of top level functions that are highly optimized for performance.

Usage

Install the package from npm registry as follows:

npm i indicative-parser

# yarn
yarn add indicative-parser

and then use it as follows:

import { rulesParser } from 'indicative-parser'

rulesParser({
  username: 'required',
  'account.type': 'required|in:email,social'
})

Above code outputs the following tree.

{
  "username": {
    "type": "literal",
    "rules": [
      {
        "name": "required",
        "args": []
      }
    ]
  },
  "account": {
    "rules": [],
    "type": "object",
    "children": {
      "type": {
        "type": "literal",
        "rules": [
          {
            "name": "required",
            "args": []
          },
          {
            "name": "in",
            "args": [
              "email",
              "social"
            ]
          }
        ]
      }
    }
  }
}

Typed schema

The parser also allows creating declarative schema that has static type information along with the parsed tree. The type information is really helpful for Typescript projects.

import { rulesParser, t } from 'indicative-parser'

rulesParser(t.schema({
  username: t.string(),
  account: t.object().members({
    type: t.string(validations.in(['email', 'social']))
  })
}))

Keywords

FAQs

Package last updated on 25 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc